<?php

/*
 * 用户相关操作.
 * 
 * @author Kun Xu <kunx@jumei.com>
 */

/**
 * Description of UserController
 *
 * @author 坤
 */
class UserController extends Controller
{

    public function actions()
    {
        return array(
//            'getVerifyCode' => array(
            'getVerifyCode' => array(
                'class' => 'system.web.widgets.captcha.CCaptchaAction',
                'minLength' => 4,
                'maxLength' => 4,
            ),
        );
    }

    /**
     * 用户登录，并没有使用数据库验证
     */
    public function actionLogin()
    {
        $this->layout = 'column2';
        $model = new LoginForm;
        $this->validateAjax($model);
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            if ($model->validate() && $model->login()) {
                $url = $this->createUrl('/admin/upyunadmin/list');
                $this->redirect($url);
            }
        }
        $data = array(
            'model' => $model,
        );
        $this->render('login', $data);
    }

    private function validateAjax($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] == 'userlogin-ajax') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }

}
